np.array()
np.array(input)
Casts an iterable (bpd.Series, bpd.Index, and list) into a NumPy array.
- Input:
- input : ser, df.index, df.columns, list
- Name of series/.columns/list to cast to an array.
- Returns:
- An array with the same elements as the input.
- Return Type:
- Array
Series to Array
weight_ser = pets.get('Weight')
weight_ser
- 040
- 11.5
- 215
- 380
- 425
- 51
- 60.25
- 710
weight_arr = np.array(weight_ser)
weight_arr
array([40. , 15. , 20. , 80. , 25. , 1. , 0.25])
bpd.Index to Array
pets_idx = pets.set_index('ID').index
pets_idx
Index(['dog_001', 'cat_001', 'cat_002', 'dog_002', 'dog_003', 'ham_001', 'ham_002', 'cat_003'], dtype='object', name='ID')
#df.index to array
pets_idx_arr = np.array(pets.set_index('ID').index)
pets_idx_arr
array(['dog_001', 'cat_001', 'cat_002', 'dog_002', 'dog_003', 'ham_001', 'ham_002', 'cat_003'], dtype=object)
pets_cols = pets.columns
pet_cols
Index(['ID', 'Species', 'Color', 'Weight', 'Age', 'Is_Cat', 'Owner_Comment'], dtype='object')
#df.columns to array
pets_cols_arr = np.array(pets.columns)
pet_cols_arr
array(['ID', 'Species', 'Color', 'Weight', 'Age', 'Is_Cat', 'Owner_Comment'], dtype=object)
List to Array
pets_list = ["dog", "cat", "hamster", "cat", "cat", "dog", "dog", "hamster", "hamster"]
pet_list
["dog", "cat", "hamster", "cat", "cat", "dog", "dog", "hamster", "hamster"]
pets_list_arr = np.array(pets_list)
pet_list_arr
array(["dog", "cat", "hamster", "cat", "cat", "dog", "dog", "hamster", "hamster"], dtype=object)